import { createApp } from "vue";
import { createPinia } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";

import "./styles/index.scss";
import "./styles/tailwind.css";
import "./permission";
import App from "./App.vue";
import router from "./router";
import { errorHandler } from "./error";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import "element-plus/theme-chalk/dark/css-vars.css";
import { useStoreTemplate } from "@renderer/store/modules/template";

const whiteList = ["/login", "/:pathMatch(.*)*"];
router.beforeEach((to, from, next) => {
  const storeTemplate = useStoreTemplate();
  if (whiteList.findIndex((item) => item == to.path) == -1) {
    if (!storeTemplate.$state.token) {
      next("/login");
    } else {
      next();
    }
  } else {
    next();
  }
});

const app = createApp(App);
const store = createPinia();
store.use(piniaPluginPersistedstate);
app.use(router);
app.use(store);
app.use(ElementPlus);
errorHandler(app);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

app.mount("#app");
